package com.pulaien.shop.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.pulaien.common.core.controller.BaseController;
import com.pulaien.common.core.domain.AjaxResult;
import com.pulaien.common.domain.ResponseCode;
import com.pulaien.common.utils.ConstantsUtil;
import com.pulaien.common.utils.oss.AliOssUploadUtils;
import com.pulaien.common.utils.oss.ConstantPropertiesUtil;
import com.pulaien.system.domain.MessageReminding;
import com.pulaien.system.domain.dto.ExtUploadFileDto;
import com.pulaien.system.service.IMessageRemindingService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.security.SecureRandom;

/**
 * 消息提醒Controller
 *
 * @author pulaien
 */
@RestController
@RequestMapping("/shop/messageReminding")
@Api(tags = "商城端消息提醒", description = "消息提醒")
public class ShopMessageRemindingController extends BaseController
{
    @Autowired
    private IMessageRemindingService messageRemindingService;


    @ApiOperation("上传")
    @RequestMapping("/uploadFile")
    @ResponseBody
    public AjaxResult uploadFile(HttpServletRequest request, HttpServletResponse response, ExtUploadFileDto upfile) {
        try {
            if(upfile==null){
                return AjaxResult.error(10143);
            }else{
                String fileName = upfile.getFile().getOriginalFilename();
                String extraName = fileName.substring(fileName.lastIndexOf("."));
                InputStream inputStream = upfile.getFile().getInputStream();
                SecureRandom random = new SecureRandom();
                String imageName = "oss_" + System.currentTimeMillis() + "_" + random.nextInt(10000) + extraName;
                //上传oss
                AjaxResult serviceResult = AliOssUploadUtils.uploadOss(imageName,inputStream);
                if(ConstantsUtil.RESPONSE_CODE_SUCCESS.equals(serviceResult.get(ConstantsUtil.CODE_TAG))){
                    JSONObject json = new JSONObject();
                    JSONObject jsonObject = JSONObject.parseObject(serviceResult.get(ConstantsUtil.DATA_TAG).toString());
                    json.put("filePath", ConstantPropertiesUtil.HOSTURL+jsonObject.get("filePath").toString());
                    json.put("fileName",fileName);
                    return AjaxResult.success(json);
                }else{
                    return AjaxResult.error(ResponseCode.UPLOAD_FAIL);
                }
            }
        } catch (IOException e) {
            return AjaxResult.error(ResponseCode.UPLOAD_FAIL);
        }
    }

    @ApiOperation("客户发送消息")
    @RequestMapping(value = "/sendMessage",method = RequestMethod.POST)
    @ResponseBody
    public AjaxResult sendMessage(@RequestBody MessageReminding message){

        if(CollectionUtil.isNotEmpty(message.getFileNameList())) {
            StringBuilder sb = new StringBuilder();
            message.getFileNameList().forEach(
                    s -> {
                        sb.append(s).append(",");
                    }
            );
            String str = sb.toString();
            String sub = str.substring(0, str.length() - 1);
            message.setFileName(sub);
        }

        if(CollectionUtil.isNotEmpty(message.getFilePathList())) {
            StringBuilder sb = new StringBuilder();
            message.getFilePathList().forEach(
                    s -> {
                        sb.append(s).append(",");
                    }
            );
            String str = sb.toString();
            String sub = str.substring(0, str.length() - 1);
            message.setFilePath(sub);
        }


        if(CollectionUtil.isNotEmpty(message.getReplyPictureList())) {
            StringBuilder sb = new StringBuilder();
            message.getReplyPictureList().forEach(
                    s -> {
                        sb.append(s).append(",");
                    }
            );
            String str = sb.toString();
            String sub = str.substring(0, str.length() - 1);
            message.setReplyPicture(sub);
        }



        return messageRemindingService.sendMessageByCus(message);
    }


    /**
     * 获取消息提醒详情
     */
    @ApiOperation(value = "获取消息提醒详情")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(messageRemindingService.selectMessageRemindingById(id));
    }

}
